package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.IUserService;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/findPage")
    public List<User>  findPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "10") Integer pageSize){
        return userService.findPage(page,pageSize);
    }

    /**
     * 下载Jxl
     */
    @GetMapping("/downLoadXlsByJxl")
    public void exportByJxl(HttpServletResponse response) throws WriteException, IOException {
//        userService.downLoadXlsByJxl(response);
    }

    /**
     * 使用poi导出带样式的文件
     * @param response
     * @throws WriteException
     * @throws IOException
     */
    @GetMapping("/downLoadXlsxByPoi")
    public void exportByPoi(HttpServletResponse response) throws WriteException, IOException {
        userService.downLoadXlsxByPoi(response);
    }

    @GetMapping(value = "/download",name = "导出用户详细信息")
    public void downLoadUserInfoWithTemplate(Long id, HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 使用POI实现带模板导出（含图片）
//        userService.downLoadUserInfoWithTemplate(id,request,response);
        // 使用EasyPOI实现带模板导出（含图片）
        userService.downLoadUserInfoWithTemplateForEasyPoi(id,request,response);
    }

    /**
     * 下载员工合同
     * @param id 员工id
     * @param response 用于导出文件时的响应对象
     */
    @GetMapping(value = "/downloadContract", name = "下载员工合同")
    public void downloadContract(Long id, HttpServletResponse response) throws Exception {
        // 使用POI实现word文件的导出
//        userService.downLoadContract(id, response);
        // 使用EasyPOI实现word文件的导出
        userService.downLoadContractByEasyPOI(id, response);
    }

    /**
     * 使用EasyPOI的方式进行用户信息导出
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/downLoadWithEasyPOI", name = "使用EasyPOI的方式进行用户信息导出")
    public void downLoadWithEasyPOI(HttpServletResponse response) throws Exception {
        userService.downLoadWithEasyPOI(response);
    }

    /**
     * 使用EasyPOI实现表格数据的导入
     * @param file 上传的表格文件
     * @throws Exception
     */
    @PostMapping(value = "/uploadExcel", name = "上传用户数据")
    public void uploadExcel(MultipartFile file) throws Exception{
        userService.uploadExcelWithEasyPOI(file);
    }

}
